package jjn.round1;

/**
 * @author Jjn
 * @since 2022/8/20 22:49
 */
public class LeetCode167_TwoSumInputIsSorted {
    public int[] twoSum(int[] numbers, int target) {
        for (int i = 0; i < numbers.length; i++) {
            int number = numbers[i];
            int start, end;
            if (target - number >= number) {
                start = i + 1;
                end = numbers.length - 1;
            } else {
                start = 0;
                end = i - 1;
            }
            int index = findNumber(numbers, target - number, start, end);
            if (i != index && index != -1) {
                return new int[]{i + 1, index + 1};
            }
        }
        return new int[]{0, 1};
    }

    private int findNumber(int[] numbers, int target, int start, int end) {
        int low = start, high = end;
        while (low <= high) {
            int mid = low + (high - low) / 2;
            if (numbers[mid] == target) {
                return mid;
            } else if (numbers[mid] > target) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return -1;
    }
}
